package com.example.demo.oss;

import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;

/**
 * @author: yby
 * @desc:
 * @date: 2024/9/20 9:34
 */
public class PptUtil {

   public static void main(String[] args) throws IOException {
      String inputPath = "D:\\资料\\个人工作\\辅助资料\\11.pptx";
      //pptToImg(path,"minioPre");
      //String inputPath = "path/to/your/presentation.pptx";
      String outputDir = "D:\\资料\\个人工作\\辅助资料\\";

   }

   /**
    * @param path pdf文件地址  例如 pdf/2022/04/xx.pdf
    * @return 返回文件全路径名数组
    */
   public static ArrayList<String> pptToImg(String path, String minioPre) throws IOException {
      String format = "png"; // 定义要保存的图片格式
      File file = new File("D:\\资料\\个人工作\\辅助资料\\11.pptx");
      InputStream fileInputStream = new FileInputStream(file);
      //InputStream fileInputStream = minioTemplate.getFileInputStream(fileProperties.getBucketName(), path); // 创建文件输入流读取PPT文件
      String suffixName = path.substring(path.lastIndexOf(".") + 1);
      ArrayList<String> fileNames = new ArrayList<>(); // 创建存储图片路径的列表
      if(suffixName.equals("pptx")){
         XMLSlideShow ppt = new XMLSlideShow(fileInputStream); // 使用XMLSlideShow类加载PPT文件
         int slideIndex = 0; // 幻灯片索引，用于命名图片
         for (XSLFSlide slide : ppt.getSlides()) {
            Dimension pageSize = slide.getSlideShow().getPageSize(); // 获取幻灯片的大小
            BufferedImage image = new BufferedImage(pageSize.width, pageSize.height, BufferedImage.TYPE_INT_RGB); // 创建缓冲图像
            Graphics2D graphics = image.createGraphics(); // 创建2D绘图对象
            graphics.setTransform(new AffineTransform()); // 设置绘图对象的转换
            //绘制幻灯片内容到缓冲图像
            slide.draw(graphics);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageOutputStream imOut = ImageIO.createImageOutputStream(bos);
            ImageIO.write(image, format, imOut);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(bos.toByteArray());
            inputStream.close();
            // 保存图片
            String fileName = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf(".")) + "/" + slideIndex; // 根据路径和文件名规则生成图片的文件名
            String newPath = "D:\\资料\\个人工作\\辅助资料";
            //String newPath = minioTemplate.uploadObjectByInputStream(fileProperties.getBucketName(), inputStream, format, fileName);
            fileNames.add(minioPre + "/" + newPath);
            slideIndex++; // 幻灯片索引自增
         }
         //ppt.close(); // 关闭PPT文件
      }else if (suffixName.equals("ppt")){
         HSLFSlideShow ppt = new HSLFSlideShow(fileInputStream);
         int slideIndex = 0; // 幻灯片索引，用于命名图片
         for (HSLFSlide slide : ppt.getSlides()) {
            Dimension pageSize = slide.getSlideShow().getPageSize(); // 获取幻灯片的大小
            BufferedImage image = new BufferedImage(pageSize.width, pageSize.height, BufferedImage.TYPE_INT_RGB); // 创建缓冲图像
            Graphics2D graphics = image.createGraphics(); // 创建2D绘图对象
            graphics.setTransform(new AffineTransform()); // 设置绘图对象的转换
            //绘制幻灯片内容到缓冲图像
            slide.draw(graphics);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageOutputStream imOut = ImageIO.createImageOutputStream(bos);
            ImageIO.write(image, format, imOut);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(bos.toByteArray());
            inputStream.close();
            // 保存图片
            String fileName = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf(".")) + "/" + slideIndex; // 根据路径和文件名规则生成图片的文件名
            String newPath = "D:\\资料\\个人工作\\辅助资料";
            //String newPath = minioTemplate.uploadObjectByInputStream(fileProperties.getBucketName(), inputStream, format, fileName);
            fileNames.add(minioPre + "/" + newPath);
            slideIndex++; // 幻灯片索引自增
         }
         ppt.close(); // 关闭PPT文件
      }
      return fileNames; // 返回图片路径列表
   }

}
